package com.core.util;

import android.text.TextUtils;
import android.util.Base64;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhangyipeng on 16/5/13.
 */
public class Base64Utils {

    // 加密
    public static String encodeStr(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        if (b != null) {
//            s = new BASE64Encoder().encode(b);
//        }
        if (b != null) {
            s = Base64.encodeToString(b, Base64.DEFAULT);
        }
        return s;
    }

    // 解密
    public static String decodeStr(String s) {
        byte[] b = null;
        String result = null;
        if (!TextUtils.isEmpty(s)) {
//            BASE64Decoder decoder = new BASE64Decoder();
            try {
//                b = decoder.decodeBuffer(s);
                b = Base64.decode(s, Base64.DEFAULT);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
